﻿

#region using namespace
using System.Collections.Generic;
using System.Linq;
using DTO;
using System;
#endregion

namespace DAO
{
    public class TuKhoaTinhThanhDAO
    {
       // tim tu khoa tinh thanh dua vao ma tu khoa tinh thanh.
        public List<TuKhoaTinhThanhDTO> Search(string strInput)
        {
            TuKhoaTinhThanhDTO tukhoaTinhThanhDTO = null;
            strInput = strInput.Replace(@"'", @"''");
            List<TuKhoaTinhThanhDTO> lsttukhoatinhthanh = getAllTuKhoaTinhThanh();

            List<TuKhoaTinhThanhDTO> ilsttukhoatinhthanh = new List<TuKhoaTinhThanhDTO>();
            
            foreach (TuKhoaTinhThanhDTO tukhoa in lsttukhoatinhthanh)
            {
                if (strInput.Contains(tukhoa.TuKhoaTinhThanh) || tukhoa.TuKhoaTinhThanh.Contains(strInput))
                {
                    tukhoaTinhThanhDTO = new TuKhoaTinhThanhDTO();

                    tukhoaTinhThanhDTO.MaTuKhoaTinhThanh = tukhoa.MaTuKhoaTinhThanh;
                    tukhoaTinhThanhDTO.MaTinhThanh = tukhoa.MaTinhThanh;
                    tukhoaTinhThanhDTO.TuKhoaTinhThanh = tukhoa.TuKhoaTinhThanh;

                    ilsttukhoatinhthanh.Add(tukhoaTinhThanhDTO);
                }
            }
            return ilsttukhoatinhthanh;
        }

        //lay tat ca cac thuoc tinh cua bang tu khoa tinh thanh.
        public static List<TuKhoaTinhThanhDTO> getAllTuKhoaTinhThanh()
        {
            IList<TUKHOATINHTHANH> ilsttktinhthanh;
            List<TuKhoaTinhThanhDTO> lsttktinhthanh = new List<TuKhoaTinhThanhDTO>();
            TuKhoaTinhThanhDTO tktinhthanhDTO = null;

            using (var context = new DataContext(GlobalDatabase.ConnectString))
            {
                ilsttktinhthanh = (from tktinhthanh in context.TUKHOATINHTHANHs select tktinhthanh).ToList();
            }

            for (int i = 0; i < ilsttktinhthanh.Count; i++)
            {
                tktinhthanhDTO = new TuKhoaTinhThanhDTO();

                tktinhthanhDTO.MaTinhThanh = (int)ilsttktinhthanh[i].MaTinhThanh;
                tktinhthanhDTO.TuKhoaTinhThanh = ilsttktinhthanh[i].TuKhoaTinhThanh1;
                tktinhthanhDTO.MaTuKhoaTinhThanh = ilsttktinhthanh[i].MaTuKhoaTinhThanh;

                lsttktinhthanh.Add(tktinhthanhDTO);
            }

            return lsttktinhthanh;
        }
        
    }
}
